package com.jgdt.demo;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class AppData {
    private static final String TAG = AppData.class.getSimpleName();
    private static SharedPreferences pref;
    private static boolean checkValid() {
        if(pref==null&&AppContext.I!=null){
            pref=AppContext.I.getSharedPreferences("AppData", Context.MODE_PRIVATE);
        }
        return pref!=null;
    }
    @Deprecated
    public static SharedPreferences getPreferences() {
        if(!checkValid()) return null;
        return pref;
    }
    /**[Boolean]*/
    public static boolean putBoolean(String key, boolean value) {
        if(!checkValid()) return false;
        return pref.edit().putBoolean(key, value).commit();     
    }
    public static boolean getBoolean(String key, boolean defaultValue) {
        if(!checkValid()) return defaultValue;
        return pref.getBoolean(key, defaultValue);
    }
    /**[Integer]*/
    public static boolean putInt(String key, int value) {
        if(!checkValid()) return false;
        return pref.edit().putInt(key, value).commit();     
    }
    public static int getInt(String key, int defaultValue) {
        if(!checkValid()) return defaultValue;
        return pref.getInt(key, defaultValue);
    }
    /**[Long]*/
    public static boolean putLong(String key, long value) {
        if(!checkValid()) return false;
        return pref.edit().putLong(key, value).commit();     
    }
    public static long getLong(String key, long defaultValue) {
        if(!checkValid()) return defaultValue;
        return pref.getLong(key, defaultValue);
    }
    /**[Float]*/
    public static boolean putFloat(String key, float value) {
        if(!checkValid()) return false;
        return pref.edit().putFloat(key, value).commit();
    }
    public static float getFloat(String key, float defaultValue) {
        if(!checkValid()) return defaultValue;
        return pref.getFloat(key, defaultValue);
    }
    /**[String]*/
    public static boolean putString(String key, String value) {
        if(!checkValid()) return false;
        return pref.edit().putString(key, value).commit();     
    }
    public static String getString(String key, String defaultValue) {
        if(!checkValid()) return defaultValue;
        return pref.getString(key, defaultValue);
    }
    public static boolean clear() {
        if(!checkValid()) return false;
        return pref.edit().clear().commit();
    }
    
    /** 版本数据升级 */
    public static boolean upgradeVersionData(int versionCode) {
        Log.d(TAG, "[VersionData]: Start to upgrade version data["+versionCode+"]!");
        if(!checkValid()){
            Log.d(TAG, "[VersionData]: Fail to upgrade! Because check invaild !");
            return false;
        }
        int oldVersionCode=getInt("zoren.zcf.user.version", 0);
        if(oldVersionCode!=versionCode){
            boolean result=pref.edit().clear().putInt("zoren.zcf.user.version", versionCode).commit();
            Log.d(TAG, "[VersionData]: Upgrade version data finished["+oldVersionCode+"->"+versionCode+"]!");
            return result;
        }else{
            Log.d(TAG, "[VersionData]: No need to upgrade version data!");
            return true;
        }
    }
}
